package com.nantai.jiafang.nantaijiafang.activity.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.blankj.utilcode.util.StringUtils;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.Item.HeardItem_2;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhiJianItemBinding;
import com.nantai.jiafang.nantaijiafang.zhijian.fragment.InterfaceToFragment;
import com.nantai.jiafang.nantaijiafang.zhijian.fragment.ZhijianFragment_add;
import com.nantai.jiafang.nantaijiafang.zhijian.fragment.ZhijianFragment_detail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/2.
 */

public class ZhiJianAdapter extends RecyclerView.Adapter<ZhiJianAdapter.ViewHolder> {
    private Context mcontext;
    private List<HeardItem_2> mlistt;
    private int layoutidd;
    private ArrayList<Boolean> flag;
    private static InterfaceToFragment mFragmentManager;

    static class ViewHolder extends RecyclerView.ViewHolder {
        FragmentZhiJianItemBinding binding;

        public ViewHolder(FragmentZhiJianItemBinding view) {
            super(view.getRoot());
           this.binding=view;
        }
        public FragmentZhiJianItemBinding getBinding(){
            return binding;
        }
    }

    public ZhiJianAdapter(List<HeardItem_2> listt, int layoutid,InterfaceToFragment mFragmentManager) {
        flag=new ArrayList<>();
        mlistt = listt;
        layoutidd=layoutid;
        this.mFragmentManager=mFragmentManager;
        for (int i = 0; i <listt.size() ; i++) {
            if(StringUtils.isEmpty(mlistt.get(i).getIsApprove())){
                mlistt.get(i).setIsApprove("0");
            }
//            mlistt.get(i).setIsApprove("0");
            flag.add(false);
        }
    }

    public void setListData(List<HeardItem_2>  list){
        this.mlistt = list;
        flag=new ArrayList<>();
        for (int i = 0; i <mlistt.size() ; i++) {
            if(StringUtils.isEmpty(mlistt.get(i).getIsApprove())){
                mlistt.get(i).setIsApprove("0");
            }
//            mlistt.get(i).setIsApprove("0");
            flag.add(false);
        }
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mcontext == null) {
            mcontext=parent.getContext();
        }
        FragmentZhiJianItemBinding binding= DataBindingUtil.inflate(LayoutInflater.from(mcontext),layoutidd,parent,false);
        return new ViewHolder(binding);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.getBinding().setItem(mlistt.get(position));
        holder.getBinding().lookdetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFragmentManager.toZhijianFragment_detail(mlistt.get(position).getID());

            }
        });
        holder.getBinding().cardZhijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFragmentManager.toZhijianFragment_add(mlistt.get(position));

            }
        });

        holder.getBinding().ckSelect.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器，传入参数null
       // Log.e(TAG, "onBindViewHolder: "+position );
        holder.getBinding().ckSelect.setChecked(flag.get(position));//用数组中的值设置CheckBox的选中状态

        //再设置一次CheckBox的选中监听器，当CheckBox的选中状态发生改变时，把改变后的状态储存在数组中
        holder.getBinding().ckSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                flag.set(position,b);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlistt.size();
    }
    public ArrayList<Boolean> getFlag(){
        return flag;
    }
    public String getGuid(){
        if (flag==null)
            return "";
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i <flag.size() ; i++) {
            if (flag.get(i)) {
                buffer.append(mlistt.get(i).getGuid()+",");
            }
        }
        if (buffer.toString().length()>0) {
            buffer.deleteCharAt(buffer.length() - 1);
        }
        return buffer.toString();
    }
    public void removeitem(int position){
        mlistt.remove(position);
        flag.remove(position);
        notifyDataSetChanged();
    }
    public void onApproveitem(boolean isApprove){
        for (int i = 0; i <flag.size() ; i++) {
            if (flag.get(i)){
                if (isApprove)
                    mlistt.get(i).setIsApprove("1");
                else
                    mlistt.get(i).setIsApprove("0");
            }
        }
        notifyDataSetChanged();
    }
}